package com.ziyun.erp.web.modules.promotion.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.member.UserTypeEnum;
import com.ziyun.erp.common.e.promotion.PolicyTypeEnum;
import com.ziyun.erp.common.e.promotion.PurviewEnum;
import com.ziyun.erp.common.e.promotion.ScopeTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goods.entity.GoodsBrandEntity;
import com.ziyun.erp.modules.goods.service.GoodsBrandService;
import com.ziyun.erp.modules.member.service.ConfigMemberLevelService;
import com.ziyun.erp.modules.promotion.entity.PromotionPolicyConfigEntity;
import com.ziyun.erp.modules.promotion.service.PromotionPolicyConfigService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 促销优惠配置信息
 *
 * @author jiangli
 * @email
 * @date 2018-09-17 09:34:00
 */
@RestController
@RequestMapping("promotion/promotionpolicyconfig")
public class PromotionPolicyConfigController {
    @Autowired
    private PromotionPolicyConfigService promotionPolicyConfigService;
    @Autowired
    private GoodsBrandService goodsBrandService;
    @Autowired
    private ConfigMemberLevelService configMemberLevelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("promotion:promotionpolicyconfig:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = promotionPolicyConfigService.queryPage(params);
        // 存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("policyTypeEnum", JsonUtils.enumToList(PolicyTypeEnum.class));//促销方式
        mapEnum.put("scopeTypeEnum", JsonUtils.enumToList(ScopeTypeEnum.class));//活动范围类型
        mapEnum.put("userTypeEnum", JsonUtils.enumToList(UserTypeEnum.class));//用户类型类型
        mapEnum.put("purviewEnum", JsonUtils.enumToList(PurviewEnum.class));//适用平台
        return R.ok().put("page", page).put("userData", mapEnum);
    }

    /*
     获取商品类型,品牌类型
     */
    @RequestMapping("/getGoodsTypes")
    public R getGoodsTypes() {
//        List<Map<String, Object>> productTypes = promotionPolicyConfigService.getAllGoodsType();
        List<GoodsBrandEntity> goodsBrandList = goodsBrandService.selectList(new EntityWrapper<GoodsBrandEntity>().eq("seller_id", 1));
//        return R.ok().put("productTypes", productTypes).put("goodsBrandList", goodsBrandList);
        return R.ok().put("goodsBrandList", goodsBrandList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("promotion:promotionpolicyconfig:info")
    public R info(@PathVariable("id") Integer id) {
        PromotionPolicyConfigEntity promotionPolicyConfig = promotionPolicyConfigService.selectById(id);
        List<GoodsBrandEntity> goodsBrandList = goodsBrandService.selectList(new EntityWrapper<GoodsBrandEntity>().eq("seller_id", 1));

        return R.ok().put("promotionPolicyConfig", promotionPolicyConfig).put("goodsBrandList", goodsBrandList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("promotion:promotionpolicyconfig:save")
    public R save(@RequestBody PromotionPolicyConfigEntity promotionPolicyConfig) {
        ValidatorUtils.validateEntity(promotionPolicyConfig, AddGroup.class);
        SysUserEntity curentUser = UserUtils.getCurentUser();
        promotionPolicyConfig.setOperation(curentUser.getUsername());
        return promotionPolicyConfigService.insertEntity(promotionPolicyConfig);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("promotion:promotionpolicyconfig:update")
    public R update(@RequestBody PromotionPolicyConfigEntity promotionPolicyConfig) {
        return promotionPolicyConfigService.updateEntityById(promotionPolicyConfig);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("promotion:promotionpolicyconfig:delete")
    public R delete(@RequestBody Integer[] ids) {
        return promotionPolicyConfigService.deletebyIds(ids);

    }

}
